package com.lujianfei.recyclerview.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.lujianfei.recyclerview.R;
import com.lujianfei.recyclerview.entity.ListItem;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

public class WaterfallBannerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    public static final int TYPE_ITEM_BANNER = 0;
    public static final int TYPE_ITEM = 1;

    List<ListItem> listItemList;
    private int headCount = 1;

    public WaterfallBannerAdapter(List<ListItem> listItemList) {
        this.listItemList = listItemList;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view;
        switch (viewType) {
            case TYPE_ITEM_BANNER:{
                //头视图需要拉伸至满屏
                view = LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.adapter_header, parent, false);
                StaggeredGridLayoutManager.LayoutParams layoutParams = (StaggeredGridLayoutManager.LayoutParams) view.getLayoutParams();
                layoutParams.setFullSpan(true);
                view.setLayoutParams(layoutParams);
                return new BannerViewHolder(view);
            }
            case TYPE_ITEM: {
                view = LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.adapter_item, parent, false);
                return new ItemViewHolder(view);
            }
            default:{
                return null;
            }
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        ListItem listItem = this.listItemList.get(position);
        switch (getItemViewType(position)){
            case TYPE_ITEM_BANNER:{
                BannerViewHolder itemViewHolder = (BannerViewHolder) holder;
                itemViewHolder.title.setTextColor(0xffffffff);
                itemViewHolder.itemView.setBackgroundColor(listItem.getColor());
            }
            break;
            case TYPE_ITEM:{
                ItemViewHolder itemViewHolder = (ItemViewHolder) holder;
                itemViewHolder.title.setText(listItem.getTitle());
                itemViewHolder.summary.setText(listItem.getSummary());
                itemViewHolder.itemView.setBackgroundColor(listItem.getColor());
                int height = 0;
                switch ((position + headCount) % 4) {
                    case 0:
                    case 2: {
                        height = 800;
                    }
                    break;
                    case 1:
                    case 3: {
                        height = 500;
                    }
                    break;
                }
                ViewGroup.LayoutParams layoutParams = itemViewHolder.itemView.getLayoutParams();
                layoutParams.height = height;
                itemViewHolder.itemView.setLayoutParams(layoutParams);
            }
            break;
        }
    }

    @Override
    public int getItemViewType(int position) {
        return listItemList.get(position).getType();
    }

    @Override
    public int getItemCount() {
        return this.listItemList.size();
    }

    static class BannerViewHolder extends RecyclerView.ViewHolder {
        View contentView;
        ImageView iv_header;
        TextView title;

        public BannerViewHolder(View view) {
            super(view);
            contentView = view;
            iv_header = view.findViewById(R.id.iv_header);
            title = view.findViewById(R.id.title);
        }
    }

    static class ItemViewHolder extends RecyclerView.ViewHolder {
        View contentView;
        TextView title;
        TextView summary;

        public ItemViewHolder(View view) {
            super(view);
            contentView = view;
            title = view.findViewById(R.id.title);
            summary = view.findViewById(R.id.summary);
        }
    }
}
